//平台费率
const platformFeeRate = 0.08;

/**
 * 获取运费json数据
 * @returns {*}
 */
var getFreightJsonData = function () {
    var version = parseInt(1000000000 * Math.random());
    var ajax = $.ajax({url: "data/freight.json?v=" + version, async: false});
    return ajax["responseJSON"];
};


/**
 * 平台使用费
 * @param sellPrice 定价
 * @returns {*}
 */
var platformFeeCal = function (sellPrice) {
    return funMul(sellPrice, platformFeeRate);
};
/**
 * 定价计算  通过  定价=成本（进价+藏价+平台费）+ 利润   推导得出
 * @param cost 进价（经过数量计算的）
 * @param hideFee 藏价
 * @param rate 利润率 小数形式
 */
var sellingPriceCal = function (cost, hideFee, rate) {
    let x = funMul(funAdd(cost, hideFee), funAdd(1, rate));//分子
    let y = funSub(funSub(1, platformFeeRate), funMul(platformFeeRate, rate))
    return funDiv(x, y);
};
/**
 * 续重价格计算
 * @param weight 重量
 * @param basicWeight 基础重量
 * @param nextWeight 续重
 * @param nextWeightFee 续费
 * @returns {*}
 */
var nextWeightPrice = function (weight, basicWeight, nextWeight, nextWeightFee) {
    //续重
    let temp = funDiv(funSub(weight, basicWeight), nextWeight);
    temp = Math.ceil(temp);
    return funMul(temp, nextWeightFee);
};

/**
 * 超重价格计算
 * @param weight
 * @param overWeight
 * @param overNextWeight
 * @param overNextWeightFee
 * @returns {*}
 */
var overWeightPrice = function (weight, overWeight, overNextWeight, overNextWeightFee) {
    let temp = funDiv(funSub(weight, overWeight), overNextWeight);
    temp = Math.ceil(temp);
    return funMul(temp, overNextWeightFee)
};

/**
 * 运费
 * @param weight 重量
 * @param basicWeight
 * @param basicWeightFee
 * @param nextWeight
 * @param nextWeightFee
 * @param overWeight
 * @param overNextWeight
 * @param overNextWeightFee
 */
var fright = function (weight, basicWeight, basicWeightFee, nextWeight, nextWeightFee, overWeight, overNextWeight, overNextWeightFee) {
    let fee = basicWeightFee;
    if (weight > basicWeight) {
        if (overWeight != null && weight > overWeight) {
            //計算超重
            //续重
            fee = funAdd(fee, nextWeightPrice(overWeight, basicWeight, nextWeight, nextWeightFee));
            // 超重
            fee = funAdd(fee, overWeightPrice(weight, overWeight, overNextWeight, overNextWeightFee));
        } else {
            // 不计算超重
            //续重
            fee = funAdd(fee, nextWeightPrice(weight, basicWeight, nextWeight, nextWeightFee));
        }
    }
    return fee;
};

/**
 * 平台运费
 * @param weight
 * @param obj
 * @returns {*}
 */
var planFormFrightCal = function (weight, obj) {
    let pBasicWeight = obj.pBasicWeight;
    let pBasicWeightFee = obj.pBasicWeightFee;
    let pNextWeight = obj.pNextWeight;
    let pNextWeightFee = obj.pNextWeightFee;
    let pOverWeight = obj.pOverWeight;
    let pOverNextWeight = obj.pOverNextWeight;
    let pOverNextWeightFee = obj.pOverNextWeightFee;
    let pFright = fright(weight, pBasicWeight, pBasicWeightFee, pNextWeight, pNextWeightFee, pOverWeight, pOverNextWeight, pOverNextWeightFee);
    return pFright;
};
/**
 * 买家运费
 * @param weight
 * @param obj
 */
var customerFrightCal = function (weight, obj) {
    let cBasicWeight = obj.cBasicWeight;
    let cBasicWeightFee = obj.cBasicWeightFee;
    let cNextWeight = obj.cNextWeight;
    let cNextWeightFee = obj.cNextWeightFee;
    let cOverWeight = obj.cOverWeight;
    let cOverNextWeight = obj.cOverNextWeight;
    let cOverNextWeightFee = obj.cOverNextWeightFee;
    let cFright = fright(weight, cBasicWeight, cBasicWeightFee, cNextWeight, cNextWeightFee, cOverWeight, cOverNextWeight, cOverNextWeightFee);
    return cFright;
}

/**
 * 藏价计算
 * @param weight 重量
 * @param basicWeight 首重
 * @param basicWeightFee 首重藏价
 * @param nextWeight 续重
 * @param nextWeightFee 续重藏价
 * @returns {*}
 */
var hideFeeCal = function (weight, obj) {
    let planFormFright = planFormFrightCal(weight, obj);
    let customerFright = customerFrightCal(weight, obj);
    let fee = funSub(planFormFright, customerFright);
    return fee;
};
/**
 * 置0
 * @param fee
 * @returns {*}
 */
var toZero = function (fee) {
    if (fee < 0) {
        fee = 0
    }
    return fee;
};

/**
 * 各地区售价
 * @param cost 进价
 * @param weight 重量
 * @param rate 利润率
 * @param number 数量
 * @returns {Array}
 */
var dataJsonByOne = function (cost, weight, rate, number) {
    let freightJsonData = getFreightJsonData();
    let data = [];
    //各国家计算
    $.each(freightJsonData, function (key, val) {
        let thatcost = cost;
        let thatweight = weight;
        let exchangeRate = val.rate; //汇率
        let freight = val.freight;  //运费规则
        //计算各地区运费 和 定价
        $.each(freight, function (freKey, freVal) {
            let countryData = {};
            countryData.countryName = val.name;
            countryData.position = freKey; //地区
            countryData.currency = val.currency; //币制
            countryData.basicWeight = freVal.basicWeight;
            countryData.basicWeightFee = freVal.basicWeightFee;
            countryData.nextWeight = freVal.nextWeight;
            countryData.nextWeightFee = freVal.nextWeightFee;
            countryData.interestRate = funMul(rate, 100) + '%';//已知利润率
            countryData.exchangeRate = exchangeRate; //汇率
            let cost = funMul(funMul(thatcost, number), exchangeRate);// 当地进价
            let hideFee = toZero(hideFeeCal(funMul(thatweight, number), freVal));//藏价
            let sellingPrice = sellingPriceCal(cost, hideFee, rate);//售价
            let platformFee = platformFeeCal(sellingPrice);//平台使用费
            let costPrice = funAdd(funAdd(cost, hideFee), platformFee); //成本
            let interest = funMul(costPrice, rate); //利润
            countryData.hidePrice = hideFee;
            countryData.sellPrice = sellingPrice.toFixed(4);
            countryData.plantFee = platformFee.toFixed(4);
            countryData.planFormFright = planFormFrightCal(weight, freVal);
            countryData.customerFright = customerFrightCal(weight, freVal);
            countryData.cost = cost;
            countryData.costPrice = costPrice.toFixed(4);
            countryData.interest = interest.toFixed(4);
            data.push(countryData);
        });
    });
    return data;
};

/**
 * 利润率  由 （利润 = 定价 - 平台费 - 藏价) 推导得出
 * @param cost 进价
 * @param sellingPrice 定价
 * @param weight 重量
 * @param number 数量
 * @param freVal 运费参数
 * @returns {string | * | *}
 */
var interestRate = function (cost, sellingPrice, weight, number, freVal) {
    let costPrice = funMul(cost, number);
    let sellPrice = funMul(sellingPrice, number);
    let hideFee = toZero(hideFeeCal(funMul(weight, number), freVal));//藏价
    let x = funSub(funMul(sellPrice, funSub(1, platformFeeRate)), hideFee);
    let y = funSub(funDiv(x, costPrice), 1);
    return y.toFixed(4);
};


// 基本运算
function funAdd(a, b) {
    math.config({
        number: 'BigNumber'
    });
    return math.parser().eval(a + "+" + b);
};

function funSub(a, b) {
    math.config({
        number: 'BigNumber'
    });
    return math.parser().eval(a + "-" + b);
};

function funDiv(a, b) {
    math.config({
        number: 'BigNumber'
    });
    return math.parser().eval(a + "/" + b);
}

function funMul(a, b) {
    math.config({
        number: 'BigNumber'
    });
    return math.parser().eval(a + "*" + b);
}
